#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : Jackie.Research.Wcf.Rest.Service
// 
// File : MicroblogService.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 微博服务
// 
// History : 
//			2011-03-30 15:17:35	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Threading;
using Jackie.Research.Wcf.Rest.Domain;

#endregion

namespace Jackie.Research.Wcf.Rest.Service
{
    /// <summary> 
    /// 微博服务
    /// </summary>
    [ServiceContract] //标记服务契约
    //标记服务能否在Asp.Net兼容模式下运行
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    //指定服务协定实现的内部执行行为,设置新服务对象合适创建
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MicroblogService
    {
        private static int currentId;

        public static readonly ConcurrentDictionary<int, MicroblogInfo> microblogs =
            new ConcurrentDictionary<int, MicroblogInfo>();

        /// <summary>
        /// 获取所有微博列表
        /// </summary>
        /// <returns></returns>
        [WebGet(UriTemplate = "")]
        public List<MicroblogInfo> GetCollection()
        {
            return microblogs.Values.ToList();
        }

        /// <summary>
        /// 新建一个微博
        /// </summary>
        /// <param name="microblog">微博实体</param>
        /// <returns></returns>
        [WebInvoke(UriTemplate = "", Method = "POST")]
        public MicroblogInfo Create(MicroblogInfo microblog)
        {
            microblog.Id = Interlocked.Increment(ref currentId);
            microblog.PublishTime = DateTime.Now;
            microblogs.TryAdd(microblog.Id, microblog);
            return microblog;
        }

        [WebGet(UriTemplate = "{id}")]
        public MicroblogInfo Get(string id)
        {
            return microblogs[int.Parse(id)];
        }

        [WebInvoke(UriTemplate = "{id}",Method = "DELETE")]
        public void Delete(string id)
        {
            MicroblogInfo blog;
            microblogs.TryRemove(int.Parse(id), out blog);
        }
    }
}